﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using WebSysCore.JSons;
using WebSysCore;
using WebSysCore.Helpers;
using HSchSchedule.Utils;

namespace HSchSchedule.AjaxPages
{
    public partial class AjaxGetXMLSubject : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod(true)]
        public static string GetXMLSubjects()
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    string xmlFilePath = HttpContext.Current.Server.MapPath("datafile.txt");
                    string[] arr = xmlFilePath.Split('\\');
                    string rootUrl = "";
                    for (int i = 0; i < arr.Length - 2; i++)
                    {
                        rootUrl += arr[i]+"\\";
                    }
                    xmlFilePath = rootUrl + "SubjectData\\subjectconfigweb.xml";
                    XSubjectList xsubjectList = XSubjectList.FromXML(xmlFilePath);
                    List<JXSubject> lstJX = new List<JXSubject>();
                    for (int i = 0; i < xsubjectList.xSubjectList.Length; i++)
                    {
                        JXSubject jx = AssignHelp.XSubToJXSub(xsubjectList.xSubjectList[i]);
                        lstJX.Add(jx);
                    }
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    return serializer.Serialize(lstJX);

                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }

        }
    }
}
